import json
import requests
import random


# 获取查询结果后，拼接成list，并校验是否包含部分值
def getListAndCheck(rs, key, values):
    if isinstance(rs, str):
        rs_list = json.loads(rs)
    else:
        rs_list = rs
    list = []
    for l in rs_list:
        list.append(l[key])
    flag = 0
    if isinstance(values, str):
        value_list = json.loads(values)
    else:
        value_list = values
    for l in list:
        for ct in value_list:
            if str(l) == ct:
                flag = 1
                break
    return str(flag)


# 检查某个key是否包含对应的值
def checkValue(rs, key, values):
    if isinstance(rs, str):
        rs_list = json.loads(rs)
    else:
        rs_list = rs
    flag = 0
    if isinstance(values, str):
        value_list = json.loads(values)
    else:
        value_list = values
    for l in rs_list:
        for ct in value_list:
            if str(l[key]) == ct:
                flag = 1
                break
    return str(flag)


# 获取查询结果后，拼接成list，返回
def getList(rs, key):
    if isinstance(rs, str):
        rs_list = json.loads(rs)
    else:
        rs_list = rs
    list = []
    for l in rs_list:
        list.append(l[key])
    return list


# 将list转化为string
def ListToStr(rs):
    if isinstance(rs, str):
        rs_list = json.loads(rs)
    else:
        rs_list = rs
    temp_list = []
    for rl in rs_list:
        if isinstance(rl, int):
            temp_list.append(str(rl))
    if temp_list == []:
        s = ','.join(rs_list)
    else:
        s = ','.join(temp_list)
    return s


# 获取查询结果后，拼接成list，再转化为string返回
def getListToStr(rs, key):
    rs_list = getList(rs, key)
    return ListToStr(rs_list)


# 获取指定环境的指定账户
def erp_username(env):
    all_environment = {
        'test': {'url': 'https://test-login.bestjlb.com', 'username': '17677777777'},
        'test2': {'url': 'https://test-login.bestjlb.com', 'username': '19946157326'},
        'test3': {'url': 'https://test-login.bestjlb.com', 'username': '19946157326'},
        'online': {'url': 'https://login.bestjlb.com', 'username': '19946157326'}
    }
    
    return all_environment.get(env).get('username')

    # 若传入username和password则为传入账户登录，若不传人username和password，则为指定账户登录


def erp_login(env, **kwargs):
    all_environment = {
        'test': {'url': 'https://test-login.bestjlb.com', 'username': '17677777777'},
        'test2': {'url': 'https://test-login.bestjlb.com', 'username': '19946157326'},
        'test3': {'url': 'https://test-login.bestjlb.com', 'username': '19946157326'},
        'online': {'url': 'https://login.bestjlb.com', 'username': '19946157326'}
    }
    url = '/orgLoginByPassword'
    
    base_url = all_environment.get(env).get('url') + url
    header = {
        'content-type': 'application/json;charset=UTF-8'
    }
    if kwargs == {}:
        
        body = {
            "mobile": all_environment.get(env).get('username'),
            "password": '123456abc'
        }
    
    else:
        body = {
            "mobile": kwargs.get('username'),
            "password": kwargs.get('password')
        }
    login_response = requests.post(url=base_url,
                                   json=body,
                                   headers=header)
    if login_response.status_code == 200:
        try:
            content = login_response._content.decode()
            json_loads = json.loads(content)
            jlb_session_id = json_loads['middle']['jlbsessionid']
            return jlb_session_id
        except:
            return ''


# 检查某个key是否包含对应的值
def getValueByCondition(rs, key1, values1, key2):
    if isinstance(rs, str):
        rs_list = json.loads(rs)
    else:
        rs_list = rs
    for l in rs_list:
        if l[key1] == values1:
            value = l[key2]
            break
    return value


# 获取查询结果后，拼接成list，返回
def getValue(rs, key):
    if isinstance(rs, str):
        rs_list = json.loads(rs)
    else:
        rs_list = rs
    list = []
    for l in rs_list:
        list.append(l[key])
    return random.choice(list)
